package com.wisemoresystem.seal.action_rest;

import java.io.File;

import javax.servlet.ServletOutputStream;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.config.Config;
import com.wisemoresystem.seal.util.img.ImgTools;
import org.ne.framework.data.util.ChkTools;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wisemoresystem.seal.model.t_file;

@Controller
@Scope("prototype")
public class ImgAction extends ProjBaseAction {

	@RequestMapping("/img/{file_id}")
	public void download(@PathVariable String file_id, String mw, String mh)
			throws Exception {

		t_file file = (t_file) oc.findObjById(file_id, t_file.class);
		ServletOutputStream os = resp.getOutputStream();
		try {
			File img = null;
			if (file != null) {
				img = new File(Config.DATA_PATH + File.separator
						+ file.getFolder(), file.getNew_file_name());
			}
			if (img == null || !img.exists()) {
				img = new File(Config.DATA_PATH, "file404.jpg");
			}

			int height = ChkTools.getInteger(mh);
			int width = ChkTools.getInteger(mw);
			height = height < 0 ? 0 : height;
			width = width < 0 ? 0 : width;

			// 设置页面不缓存
			resp.setHeader("Pragma", "No-cache");
			resp.setHeader("Cache-Control", "no-cache");
			resp.setDateHeader("Expires", 0);

			ImgTools.thumbnail_w_h(img, width, height, os);

		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("图片找不到:" + e);
		} finally {
			os.close();
		}
	}

}
